package com.wzh.sending.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.wzh.sending.R;
import com.wzh.sending.info.LocationInfo;

import java.math.BigDecimal;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/8/29.
 */
public class AllUserAdapter extends BaseAdapter {
    Context context;
    List<LocationInfo> locationInfos;
    double lot;
    double lat;

    public void setLotandLat(double lot, double lat) {
        this.lat = lat;
        this.lot = lot;
    }

    public AllUserAdapter(Context context, List<LocationInfo> locationInfos) {
        this.context = context;
        this.locationInfos = locationInfos;

    }

    @Override
    public int getCount() {
        return locationInfos.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;

        if (view == null) {
            view = View.inflate(context, R.layout.item_alluser, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        LatLng startLatlng = new LatLng(lat, lot);
        LatLng endLatlng = new LatLng(locationInfos.get(i).getLatitude(), locationInfos.get(i).getLongitude());

        if (locationInfos.get(i).getSpeed() == 0) {
            viewHolder.sudu.setVisibility(View.GONE);
        } else {
            viewHolder.sudu.setVisibility(View.VISIBLE);
            viewHolder.sudu.setText(turnMoney(locationInfos.get(i).getSpeed()) + "公里/小时");
        }
        viewHolder.time.setText("更新时间" + locationInfos.get(i).getCreatedAt());
        double mi = Double.parseDouble(String.valueOf(AMapUtils.calculateLineDistance(startLatlng, endLatlng)));
        if (mi > 500) {
            viewHolder.juli.setText(turnMoney(mi / 1000) + "千米");
        } else {
            viewHolder.juli.setText(turnMoney(mi) + "米");
        }
        viewHolder.address.setText(TextUtils.isEmpty(locationInfos.get(i).getAddRess()) ?
                "点击这一栏可以查询地址信息。" : locationInfos.get(i).getAddRess());
        return view;
    }


    static class ViewHolder {
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.juli)
        TextView juli;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.sudu)
        TextView sudu;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public BigDecimal turnMoney(double t) {
        BigDecimal bd = new BigDecimal(t);
        bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);
        return bd;
    }
}
